/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.form;

import domen.Fakultet;
import java.util.regex.PatternSyntaxException;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import model.Storage;
import model.StorageBroker;
import ui.components.modeltabele.FakultetModelTabele;

/**
 *
 * @author Vlada
 */
public class JPanelFakultetiPrikaz extends javax.swing.JPanel {

    FakultetModelTabele fmt;
    StorageBroker sb;

    /**
     * Creates new form JPanelFakultetiPrikaz
     */
    public JPanelFakultetiPrikaz() {
        Fakultet f = new Fakultet();
        sb = new StorageBroker(f);
        initComponents();
        fmt = new FakultetModelTabele();
        jTable1.setModel(fmt);

        sortiraj();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbtnObrisi = new javax.swing.JButton();
        jbtnDetalji = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jtxtFilter = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        jbtnObrisi.setText("Obrisi");
        jbtnObrisi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnObrisiActionPerformed(evt);
            }
        });

        jbtnDetalji.setText("Detalji");
        jbtnDetalji.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDetaljiActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);
        if (jTable1.getColumnModel().getColumnCount() > 0) {
            jTable1.getColumnModel().getColumn(1).setResizable(false);
        }

        jtxtFilter.setText("jTextField1");

        jLabel1.setText("Pretraga");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                            .addComponent(jtxtFilter))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jbtnDetalji, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbtnObrisi, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnDetalji)
                    .addComponent(jtxtFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jbtnObrisi)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
    }// </editor-fold>//GEN-END:initComponents
/**
     * funkcija uzima selektovani red i otvara JPanelFakultet za izmenu
     *
     * @param evt
     */
    private void jbtnDetaljiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDetaljiActionPerformed
        if (!(jTable1.getSelectedRow() == -1)) {
            int red = jTable1.getSelectedRow();

            String kljuc = (String) fmt.getValueAt(red, 0);
            Fakultet f = (Fakultet) sb.pronadjiEntitet(kljuc);

//            JPanelFakultet jpf = new JPanelFakultet(f,this);
            JPanelFakultet jpf = new JPanelFakultet(f);
            jpf.setVisible(true);
            Storage.getInstance().getGlavnaForma().ucitajPanel(jpf);
        } else {
            JOptionPane.showMessageDialog(this, "Morate da selektujete red da bi videli detalje");
        }
    }//GEN-LAST:event_jbtnDetaljiActionPerformed

    private void jbtnObrisiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnObrisiActionPerformed
        String korIme = (String) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 0);
        if (sb.izbrisiEntitet(korIme)) {
            System.out.println("Korisnik je izbrisan");
        } else {
            System.out.println("Korisnik ne moze da se izbrise!");
        }
//        osveziTabelu();
          AbstractTableModel abs = (AbstractTableModel) jTable1.getModel();
        abs.fireTableDataChanged();
    }//GEN-LAST:event_jbtnObrisiActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton jbtnDetalji;
    private javax.swing.JButton jbtnObrisi;
    private javax.swing.JTextField jtxtFilter;
    // End of variables declaration//GEN-END:variables

    public void osveziTabelu() {
        fmt.fireTableDataChanged();

    }

    /**
     * vrsi filtiranje na osnovu unesenog teksta iz text fielda
     */
    public void sortiraj() {
        jtxtFilter.setText("");
        TableModel myTableModel = jTable1.getModel();
        final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(myTableModel);
        jTable1.setRowSorter(sorter);
        jtxtFilter.getDocument().addDocumentListener(new DocumentListener() {

            private void searchFieldChangedUpdate(DocumentEvent evt) {
                String text = jtxtFilter.getText();
                if (text.length() == 0) {
                    sorter.setRowFilter(null);
                    jTable1.clearSelection();
                } else {
                    try {
                        sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                        jTable1.clearSelection();
                    } catch (PatternSyntaxException pse) {
                        pse.getMessage();
                    }
                }
            }

            @Override
            public void insertUpdate(DocumentEvent evt) {
                searchFieldChangedUpdate(evt);
            }

            @Override
            public void removeUpdate(DocumentEvent evt) {
                searchFieldChangedUpdate(evt);
            }

            @Override
            public void changedUpdate(DocumentEvent evt) {
                searchFieldChangedUpdate(evt);
            }
        });
    }

}
